package com.ssb.plugins;

import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.ssb.util.DateCalculationUtil;

import java.util.*;

/**
 * @Classname SelectOwnerPromotionFeePluginImpl
 * @Description 按照周期查询我的子级渠道商推广费明细
 * @Date 2020/4/15 18:42
 * @Created by fpc
 */
public class SelectOwnerFeeDetailPluginImpl implements CustomMethodPlugin {
    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        String userId = String.valueOf(map.get("ssCommissionDetail.w_userId"));
        String date = String.valueOf(map.get("ssCommissionDetail.w_date"));
        String pageIndex=StrUtil.doNull(String.valueOf(map.get("page")),"1");
        String pageSize=StrUtil.doNull(String.valueOf(map.get("rows")),"10");
        if(Validator.isEmpty(userId)||Validator.isEmpty(date)){
            throw new BusinessException("参数【渠道商id】或【查看周期】不能为空");
        }
        String cgsql = getCommissionGroupSql(date,userId);
        Page page = (Page)IusPluginUtil.getInstance().select("ssb", cgsql, Integer.parseInt(pageIndex), Integer.parseInt(pageSize));
        Map returnMap = new HashMap();
        returnMap.put(CustomMethodPlugin.pluginInnerReturn, page);
        returnMap.put(CustomMethodPlugin.resultMark, "T");
        return returnMap;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
    private String getCommissionGroupSql(String date,String userId){
        String column = " scd.create_user,scd.user_owner,COUNT(scd.info_id) AS num, cast(SUM(scd.amount) AS decimal(15,2)) AS amount, SUM(scd.premium) AS premium ";
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT ");
        sb.append(column);
        sb.append("FROM ss_commission_detail scd WHERE ");
        sb.append(" scd.user_id ='"+userId+"'");
        sb.append(" AND DATE_FORMAT(scd.create_time, '%Y-%m') ='"+date+"'");
        sb.append(" GROUP BY scd.create_user");
        return sb.toString();
    }

}
